package com.sf.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sf.framework.domain.Result;
import com.sf.model.AccessLog;
import com.sf.model.AccessLogExample;
import com.sf.manager.AccessLogManager;
import com.sf.service.AccessLogService;
/**
 * 报名信息Service实现类
 *
 * @author 604625
*/
@Service
public class AccessLogServiceImpl implements AccessLogService {

	private Logger logger = LoggerFactory.getLogger(getClass());
			
	@Autowired
	AccessLogManager accessLogManager;
	
	/**
	 * 按条件计数
	 * 
	 * @param example 条件
	 * 
	 * @return Result<Integer>
	 *
    */
	public Result<Integer> countByExample(AccessLogExample example){
		Result<Integer> result = new Result<Integer>(true); 
		int count = 0;
		try{
			count = accessLogManager.countByExample(example);
			result.setObj(Integer.valueOf(count));
		}
		catch(Exception e){
			result.setSuccess(false);
			result.setObj(Integer.valueOf(count));
			result.setErrorMessage("countByExample error");
			logger.error("countByExample error", e);
		}
		return result;
	}

	/**
	 * 按主键删除
	 * 
	 * @param id 
	 * 
	 * @return Result<Integer>
	 *
    */
	public Result<Integer> deleteByPrimaryKey(Integer id){
	    Result<Integer> result = new Result<Integer>(true); 
    	if(id == null){
    		throw new IllegalStateException("参数id不能为空");
    	}
		int count = 0;
		try{
			count = accessLogManager.deleteByPrimaryKey(id);
			result.setObj(Integer.valueOf(count));
		}
		catch(Exception e){
			result.setSuccess(false);
			result.setObj(Integer.valueOf(count));
			result.setErrorMessage("deleteByPrimaryKey error");
			logger.error("deleteByPrimaryKey error", e);
		}
		return result;
	}

	/**
	 * 新增
	 * 
	 * @param record 记录
	 * 
	 * @return Result<Integer>
	 *
    */
	public Result<Integer> insert(AccessLog record){
	    Result<Integer> result = new Result<Integer>(true); 
		if(record == null){
			throw new IllegalStateException("参数record不能为空");
		}
    	if(record.getId() == null){
    		throw new IllegalStateException("主键id属性不能为空");
    	}
		int count = 0;
		try{
			count = accessLogManager.insert(record);
			result.setObj(Integer.valueOf(count));
		}
		catch(Exception e){
			result.setSuccess(false);
			result.setObj(Integer.valueOf(count));
			result.setErrorMessage("insert error");
			logger.error("insert error", e);
		}
		return result;
	}
	
	/**
	 * 按条件查询
	 * 
	 * @param example 条件
	 * @param pageNum 页码
	 * @param pageSize 每页数量
	 * 
	 * @return  Page 
	 *
    */
	public Result<Page<AccessLog>> selectByExample(AccessLogExample example, Integer pageNum, Integer pageSize){
	    Result<Page<AccessLog>> result = new Result<Page<AccessLog>>(true);
		if(pageNum != null && pageNum < 1){
			throw new IllegalStateException("参数pageNum不能小于1");
		}
		if(pageSize != null && pageSize < 1){
			throw new IllegalStateException("参数pageSize不能小于1");
		}
		if((pageNum == null && pageSize != null)
			||(pageNum != null && pageSize == null)){
			throw new IllegalStateException("pageNum、pageSize必须同时为null或不为null");
		}
		if(pageNum == null && pageSize == null){ //一次查所有数据
			pageNum = 1;
			pageSize = 0;
		}
		Page<AccessLog> records = new Page<AccessLog>();
		PageHelper.startPage(pageNum, pageSize);
		try{
			records = (Page<AccessLog>)accessLogManager.selectByExampleWithBLOBs(example);
			result.setObj(records);
		}
		catch(Exception e){
			result.setSuccess(false);
			result.setObj(records);
			result.setErrorMessage("selectByExample error");
			logger.error("selectByExample error", e);
		}
		return result;
	}

	/**
	 * 按主键查询
	 * 
	 * @param id 
	 * 
	 * @return  List 
	 *
    */
    public Result<AccessLog> selectByPrimaryKey(Integer id){
	    Result<AccessLog> result = new Result<AccessLog>(true);
    	if(id == null){
    		throw new IllegalStateException("参数id不能为空");
    	}
		AccessLog record = null;
		try{
			record = accessLogManager.selectByPrimaryKey(id);
			result.setObj(record);
		}
		catch(Exception e){
			result.setSuccess(false);
			result.setErrorMessage("selectByPrimaryKey error");
			logger.error("selectByPrimaryKey error", e);
		}
		return result;
	}

	/**
	 * 按主键更新,null的字段不更新
	 * 
	 * @param record 记录
	 * @return  List 
	 *
    */
    public Result<Integer> updateByPrimaryKeySelective(AccessLog record){
	    Result<Integer> result = new Result<Integer>(true);
		if(record == null){
			throw new IllegalStateException("参数record不能为空");
		}
    	if(record.getId() == null){
    		throw new IllegalStateException("主键id属性不能为空");
    	}
		int count = 0;
		try{
			count = accessLogManager.updateByPrimaryKeySelective(record);
			result.setObj(Integer.valueOf(count));
		}
		catch(Exception e){
			result.setSuccess(false);
			result.setObj(Integer.valueOf(count));
			result.setErrorMessage("updateByPrimaryKeySelective error");
			logger.error("updateByPrimaryKeySelective error", e);
		}
		return result;
	}

	/**
	 * 按主键更新
	 * 
	 * @param record 记录
	 * @return Result<Integer>
	 *
    */
	public Result<Integer> updateByPrimaryKey(AccessLog record){
	    Result<Integer> result = new Result<Integer>(true);
		if(record == null){
			throw new IllegalStateException("参数record不能为空");
		}
    	if(record.getId() == null){
    		throw new IllegalStateException("主键id属性不能为空");
    	}
		int count = 0;
		try{
			count = accessLogManager.updateByPrimaryKey(record);
			result.setObj(Integer.valueOf(count));
		}
		catch(Exception e){
			result.setSuccess(false);
			result.setObj(Integer.valueOf(count));
			result.setErrorMessage("updateByPrimaryKey error");
			logger.error("updateByPrimaryKey error", e);
		}
		return result;
	}
	
}